<?php
/**
 * 食谱测评算法
 * Created by PhpStorm.
 * User: liao
 * Date: 2016/12/12
 * Time: 16:41
 */

namespace App\Services\Algo;

use App\Interf\Calculable;
use TestingItemService;
use App\Models\LogHelper;

class DefaultAlgo extends BaseTestingAlgo implements Calculable
{
    public $allAnswers;
    private $typeFourExpress;


    public function __construct($tid, $answerStr, $typeFourExpress = null)
    {

        parent::__construct($tid, $answerStr);
        $this->typeFourExpress = $typeFourExpress;
        $this->allAnswers = $this->convertAllAnswers();


    }


    public function calculate()
    {

        if ($this->typeFourExpress) {
            $result = $this->fourTypeArray($this->typeFourExpress, $this->answers);
            return round($result[0]['weight']);

        }


        return $this->entrance($this->tid, $this->answers);

    }


    /**
     * 将拼装后的答案转换成字符串
     * @return string
     */
    public function resultToStr()
    {
        return $this->arrToStr($this->answers);
    }


    /**
     * @param $tid
     * @param $answers
     * @return float|null
     */
    protected function entrance($tid, $answers)
    {
        return $this->getAnswerTotalScore($answers);
    }


    protected function convertAllAnswers()
    {
        if ($this->typeFourExpress) {
            $fourTypeResult = $this->fourTypeArray($this->typeFourExpress, $this->answers);
            if (count($fourTypeResult) != 1) {
                foreach ($fourTypeResult as $v) {
                    array_push($this->answers, $v);
                }
            }

        }
    }

}